/**
 * Created by 30947 on 2018/7/20.
 */
$(function(){
    getHt();
    initMap();
    mapActive();
    char1();
    char2();
    page();
    mapRestList();
})
//获取div的高度
function getHt(){
   var all_height=$(window).height();
   var div_height=all_height-84;
    $("#car_control").css("height",div_height+"px");


}
//加载地图
function initMap(){
    map = L.map('map_box', {
        center: [39.830660058696104, 116.92866163503169],
        zoom: 10,
        crs: L.CRS.EPSG3857
    });

    // 添加天地图图层
    url = "http://localhost:8090/iserver/services/map-tianditu-3/rest/maps/%E7%9F%A2%E9%87%8F%E5%BA%95%E5%9B%BE_%E5%A2%A8%E5%8D%A1%E6%89%98";
    new L.supermap.TiledMapLayer(url).addTo(map);

    url = "http://localhost:8090/iserver/services/map-tianditu-3/rest/maps/%E7%9F%A2%E9%87%8F%E4%B8%AD%E6%96%87%E6%B3%A8%E8%AE%B0_%E5%A2%A8%E5%8D%A1%E6%89%98";
    new L.supermap.TiledMapLayer(url).addTo(map);
}
//工具条点击效果
function mapActive(){
    $(".map_top>ul>li").click(function(){
        $(this).addClass("active").siblings().removeClass("active");
        $(this).find("a").addClass("active");
        $(this).find("a").parents("li").siblings().find("a").removeClass("active");
    })
}
//统计分析图
function char1() {
    fetch("/dashboard/statis", {
        method: 'GET'
    })
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            return response.json();
        })
        .then(data => {
            const volunteerCount = Number(data[0]); // 志愿者的数量
            const blindCount = Number(data[1]); // 视障人士的数量
            const guardianCount = Number(data[2]); // 亲属的数量

            var myChart = echarts.init($("#char1")[0]);
            let option = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    x: 'right',
                    y: 'bottom',
                    textStyle: {
                        color: '#ffffff',
                    },
                    data: ['视障人士', '志愿者', '亲属']
                },

                calculable: false,
                series: [
                    {
                        name: '用户类型',
                        type: 'pie',
                        radius: ['40%', '70%'],
                        itemStyle: {
                            normal: {
                                label: {
                                    show: false
                                },
                                labelLine: {
                                    show: false
                                }
                            },
                            emphasis: {
                                label: {
                                    show: true,
                                    position: 'center',
                                    textStyle: {
                                        fontSize: '20',
                                        fontWeight: 'bold'
                                    }
                                }
                            }
                        },
                        data: [
                            {value: blindCount, name: '视障人士'},
                            {value: volunteerCount , name: '志愿者'},
                            {value: guardianCount, name: '亲属'},


                        ]
                    }
                ]
            };

            myChart.setOption(option);
            // window.addEventListener('resize', function () {myChart.resize();})


        })
        .catch(error => {
            console.error("Error fetching GeoJSON data:", error);
        });

}
function char2() {
    fetch("/dashboard/blindnumber", {
        method: 'GET'
    })
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            return response.json();
        })
        .then(data => {
            const volunteerCount = Number(data[0]); // 未绑定
            const blindCount = Number(data[1]); // 绑定

            var myChart = echarts.init($("#char2")[0]);
            let option = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    x: 'right',
                    y: 'bottom',
                    textStyle: {
                        color: '#ffffff',

                    },
                    data: ['绑定亲属的盲人', '未绑定亲属的盲人']
                },

                calculable: false,
                series: [
                    {
                        name: '用户类型',
                        type: 'pie',
                        radius: ['40%', '70%'],
                        itemStyle: {
                            normal: {
                                label: {
                                    show: false
                                },
                                labelLine: {
                                    show: false
                                }
                            },
                            emphasis: {
                                label: {
                                    show: true,
                                    position: 'center',
                                    textStyle: {
                                        fontSize: '20',
                                        fontWeight: 'bold'
                                    }
                                }
                            }
                        },
                        data: [

                            {value: blindCount, name: '绑定亲属的盲人'},
                            {value: volunteerCount , name: '未绑定亲属的盲人'}



                        ]
                    }
                ]
            };

            myChart.setOption(option);
            // window.addEventListener('resize', function () {myChart.resize();})


        })
        .catch(error => {
            console.error("Error fetching GeoJSON data:", error);
        });

}
function page(){
    $("#page").Page({
        totalPages: 9,//分页总数
        liNums: 1,//分页的数字按钮数(建议取奇数)
        activeClass: 'activP', //active 类样式定义
        callBack : function(page){
            //console.log(page)
        }
    });
}
//专题图点击
function mapRestList(){
    $(".map_work>ul>li").click(function(){
        $(".map_work>ul").hide();
        $(".map_resList").show();
    })
}
//返回
function back(){
    $(".map_work>ul").show();
    $(".map_resList").hide();
}
// //右侧功能界面切换
// function rightChange(){
//     $(".map_right_top>ul>li").click(function(){
//         var ins=$(this).index();
//         $(this).addClass("li_active").siblings().removeClass("li_active");
//         $(".map_con .map_con_div").eq(ins).show().siblings().hide();
//     })
// }
